package com.example.academicsystem.server.controller.user;

import com.example.academicsystem.pojo.dto.*;
import com.example.academicsystem.pojo.vo.AdminLoginVo;
import com.example.academicsystem.pojo.vo.StudentLoginVo;
import com.example.academicsystem.pojo.vo.TeacherLoginVo;
import com.example.academicsystem.result.Result;
import com.example.academicsystem.server.service.UserService;
import com.example.academicsystem.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
        @Autowired
        private UserService userService;

    @PostMapping("/student/login")
    public Map<String, Object> studentLogin(@RequestBody StudentLoginDto studentLoginDto){
        log.info("user Login:"+ studentLoginDto);
        String encodePwd= DigestUtils.md5DigestAsHex(studentLoginDto.getPassword().getBytes(StandardCharsets.UTF_8));
        //test need to delete in production
        System.out.println(encodePwd);
        //==========
        studentLoginDto.setPassword(encodePwd);
        Map<String,Object> map = new HashMap<>();
        try {
            StudentLoginVo studentLoginVo= userService.studentLogin(studentLoginDto);
            Map<String,String> payload = new HashMap<>();
            payload.put("studentId",studentLoginVo.getStudentId());
            payload.put("name",studentLoginVo.getName());
            String token = JwtUtils.getToken(payload);
            map.put("status",true);
            map.put("msg","login success");
            map.put("token",token);
            map.put("studentId",studentLoginVo.getStudentId());
            map.put("name",studentLoginVo.getName());
        }catch(Exception e){
            log.info("login failed");
            map.put("status",false);
            map.put("msg","login failed");
        }
        return map;
    }
/*    @PostMapping("/student/login")
    public Result<StudentLoginVo> studentLogin(@RequestBody StudentLoginDto studentLoginDto){
        log.info("student login"+studentLoginDto);
        StudentLoginVo studentLoginVo = userService.studentLogin(studentLoginDto);
        return Result.success(studentLoginVo);
    }*/

    @PostMapping("/teacher/login")
    public Map<String, Object> teacherLogin(@RequestBody TeacherLoginDto teacherLoginDto){
        log.info("user Login:"+ teacherLoginDto);
        String encodePwd= DigestUtils.md5DigestAsHex(teacherLoginDto.getPassword().getBytes(StandardCharsets.UTF_8));
        teacherLoginDto.setPassword(encodePwd);
        //test need to delete in production
        System.out.println(encodePwd);
        //==========
        Map<String,Object> map = new HashMap<>();
        try {
            TeacherLoginVo teacherLoginVo= userService.teacherLogin(teacherLoginDto);
            Map<String,String> payload = new HashMap<>();
            payload.put("teacherId",teacherLoginVo.getTeacherId());
            payload.put("name",teacherLoginVo.getName());
            String token = JwtUtils.getToken(payload);

            map.put("status",true);
            map.put("msg","login success");
            map.put("token",token);
            map.put("teacherId",teacherLoginVo.getTeacherId());
            map.put("name",teacherLoginVo.getName());
        }catch(Exception e){
            log.info("login failed");
            map.put("status",false);
            map.put("msg","login failed");
        }
        return map;
    }

/*    @PostMapping("/teacher/login")
    public Result<TeacherLoginVo> teacherLogin(@RequestBody TeacherLoginDto teacherLoginDto){
        log.info("user Login:"+ teacherLoginDto);
        TeacherLoginVo teacherLoginVo= userService.teacherLogin(teacherLoginDto);
        return Result.success(teacherLoginVo);
    }*/

    @PostMapping("/admin/login")
    public Map<String, Object> adminLogin(@RequestBody AdminLoginDto adminLoginDto){
        log.info("user Login:"+ adminLoginDto);
        String encodePwd= DigestUtils.md5DigestAsHex(adminLoginDto.getPassword().getBytes(StandardCharsets.UTF_8));
        adminLoginDto.setPassword(encodePwd);
        //test need to delete in production
        System.out.println(encodePwd);
        //==========
        Map<String,Object> map = new HashMap<>();
        try {
            AdminLoginVo adminLoginVo= userService.adminLogin(adminLoginDto);
            Map<String,String> payload = new HashMap<>();
            payload.put("adminId",adminLoginVo.getAdminId());
            payload.put("name",adminLoginVo.getName());
            String token = JwtUtils.getToken(payload);

            map.put("status",true);
            map.put("msg","login success");
            map.put("token",token);
            map.put("adminId",adminLoginVo.getAdminId());
            map.put("name",adminLoginVo.getName());
        }catch(Exception e){
            log.info("login failed");
            map.put("status",false);
            map.put("msg","login failed");
        }
        return map;
    }
/*    @PostMapping("/admin/login")
    public Result<AdminLoginVo> adminLogin(@RequestBody AdminLoginDto adminLoginDto){
        log.info("user Login:"+ adminLoginDto);
        AdminLoginVo adminLoginVo= userService.adminLogin(adminLoginDto);
        return Result.success(adminLoginVo);
    }*/

    @PutMapping("/pwd/update")
    public Result pwdUpdate(@RequestBody PwdUpdateDto pwdUpdateDto){
       String encodeOldPwd= DigestUtils.md5DigestAsHex(pwdUpdateDto.getOldPwd().getBytes(StandardCharsets.UTF_8));
       String encodeNewPwd=DigestUtils.md5DigestAsHex(pwdUpdateDto.getNewPwd().getBytes(StandardCharsets.UTF_8));
        pwdUpdateDto.setNewPwd(encodeNewPwd);
        pwdUpdateDto.setOldPwd(encodeOldPwd);
       userService.pwdUpdate(pwdUpdateDto);
        return Result.success();
    }
/*    @PostMapping("/register")
    public Result userRegister(@RequestBody RegisterDto registerDto){
        userService.userRegister(registerDto);
        return Result.success();
    }*/
    @GetMapping("/test")
    public Result test(){
        return Result.success();
    }
}
